package com.tanhua.dubbo.api;


import com.tanhua.model.mongo.FocusUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

@DubboService
public class FocusUserApiImpl implements FocusUserApi {


    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveFollow(Long userId, Long followUserId) {
        //先判断是否已经关注
        Query query = Query.query(Criteria.where("userId").is(userId)
        .and("followUserId").is(followUserId));
        boolean exists = mongoTemplate.exists(query, FocusUser.class);
        if (exists){
            return;
        }
        //保存数据到focus表
        FocusUser focusUser = new FocusUser();
        focusUser.setUserId(userId);
        focusUser.setFollowUserId(followUserId);
        focusUser.setCreated(System.currentTimeMillis());
        mongoTemplate.save(focusUser);
    }

    @Override
    public void deleteFollow(Long userId, Long followUserId) {
        //删除关注
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("followUserId").is(followUserId));
        mongoTemplate.remove(query, FocusUser.class);
    }
}
